package io;

import base.Person;
import org.junit.Test;

import java.io.*;

/**
 * @author liweimin
 * @date 2021/4/6 13:35
 * @description io class test
 * @since
 */
public class IoTest {
    @Test
    public void test1() throws IOException {
        InputStream is = new FileInputStream("src\\io\\resource\\1.txt");
        byte[] bytes = new byte[100];
        int read = is.read(bytes);
        is.close();
        System.out.println(read);
        for (byte aByte : bytes) {
            System.out.println((char) aByte);
        }
    }

    @Test
    public void test2() throws IOException {
        Reader fr = new FileReader("src\\io\\resource\\1.txt");
        char[] chars = new char[100];
        int read = fr.read(chars);
        fr.close();
        System.out.println(read);
        for (char aChar : chars) {
            System.out.println(aChar);
        }
    }

    @Test
    public void test3() throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("src\\io\\resource\\1.txt");
        byte[] by = {65, 66, 67, 68, 69, 70};

        fileOutputStream.write(by);
        fileOutputStream.flush();
        fileOutputStream.close();
        FileInputStream inputStream = new FileInputStream("src\\io\\resource\\1.txt");
        byte[] bytes = new byte[100];
        inputStream.read(bytes);
        inputStream.close();
        for (byte b : bytes) {
            System.out.println((char) b);
        }
    }

    @Test
    public void test4() throws IOException {
        //追加写
        OutputStream os = new FileOutputStream("src\\io\\resource\\1.txt", true);
        byte[] by = {65, 66, 67, 68, 69, 70};
        os.write(by);
        os.flush();
        FileInputStream inputStream = new FileInputStream("src\\io\\resource\\1.txt");
        byte[] bytes = new byte[100];
        inputStream.read(bytes);
        inputStream.close();
        for (byte b : bytes) {
            System.out.println((char) b);
        }
    }

    @Test
    public void test5() throws IOException {
        BufferedReader br = null;
        BufferedWriter bw = null;
        br = new BufferedReader(new FileReader("src\\io\\resource\\1.txt"));
        bw = new BufferedWriter(new FileWriter("src\\io\\resource\\1.txt", true));
        String s = "hello world \n";
        while ((s = br.readLine()) != null) {
            bw.write(s + "hello world");
            bw.newLine();
        }
        bw.flush();

        br.close();
        bw.close();
    }


    /**
     * 使用节点流复制mp3
     *
     * @throws IOException
     */
    @Test
    public void test6() throws IOException {
        File file = new File("src\\io\\resource\\music.mp3");
        File copyFile = new File("src\\io\\resource\\music_copy.mp3");
        File copyFile1 = new File("src\\io\\resource\\music_copy1.mp3");
        InputStream is = new FileInputStream(file);
        OutputStream os = new FileOutputStream(copyFile);

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(copyFile1));


        byte[] bytes = new byte[1024];
        byte[] bytes1 = new byte[1024];
        long l = System.currentTimeMillis();
        while (is.read(bytes) != -1) {
            os.write(bytes);
        }
        long l1 = System.currentTimeMillis();
        System.out.println("节点流耗时:" + (l1 - l));
        os.close();
        is.close();


        long l2 = System.currentTimeMillis();
        while (bis.read(bytes1) != -1) {
            bos.write(bytes1);
        }
        bos.flush();
        long l3 = System.currentTimeMillis();
        System.out.println("包装流耗时:" + (l3 - l2));
        bos.close();
        bis.close();

    }

    /**
     * 转换流
     *
     * @throws IOException
     */
    @Test
    public void test7() throws IOException {
        InputStream is = new FileInputStream("src\\io\\resource\\1.txt");

        OutputStream os = new FileOutputStream("src\\io\\resource\\1.txt");

        InputStreamReader isr = new InputStreamReader(is, "GBK");

        OutputStreamWriter osr = new OutputStreamWriter(os, "GBK");

        BufferedReader br = new BufferedReader(isr);
        BufferedWriter bw = new BufferedWriter(osr);

        String str = null;

        while ((str = br.readLine()) != null) {
            bw.write(str);
            bw.newLine();
            bw.flush();
        }
        bw.close();
        br.close();
    }

    @Test
    public void test8() throws IOException {
        System.out.println("开始测试");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while ((str = br.readLine()) != null) {
            if ("e".equalsIgnoreCase(str)) {
                System.out.println("安全退出!");
                break;
            }
            System.out.println("-->" + str.toUpperCase());
            System.out.println("继续输入信息");
        }
        br.close();
    }

    /**
     * 对象流测试
     *
     * @throws IOException
     */
    @Test
    public void test9() throws IOException, ClassNotFoundException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src\\io\\resource\\data.txt"));
        Person person = new Person(17, "ssy", "女");
        oos.writeObject(person);
        //写入必须清缓冲
        oos.flush();
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src\\io\\resource\\data.txt"));
        Person person1 = (Person) ois.readObject();
        ois.close();
        System.out.println(person1);
    }

    /**
     * 随机文件流测试
     *
     * @throws FileNotFoundException
     * @throws IOException
     */
    @Test
    public void test10() throws FileNotFoundException, IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("src\\io\\resource\\data.txt", "r");

    }
}
